package com.novax.ex.activity.provider.handler.consumer;

import com.alibaba.fastjson.JSON;
import com.novax.ex.activity.infrastructure.entity.MallPrizeLottery;
import com.novax.ex.activity.infrastructure.entity.MallPrizeOrder;
import com.novax.ex.activity.provider.handler.provider.MallOrderProvider;
import com.novax.ex.activity.provider.service.MallOrderWinnerService;
import com.novax.ex.activity.provider.service.MallPrizeLotteryService;
import com.novax.ex.activity.provider.service.MallPrizeOrderService;
import com.novax.ex.common.constant.MqConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;
import java.util.Objects;

/**
 * @Description 订单消息消费者
 * @Author zhaolei
 * @Date 2/21/23 4:22 PM
 * @Version 1.0
 */
@Slf4j
@Component
public class MallOrderConsumer {

    @Resource
    private MallPrizeOrderService mallPrizeOrderService;
    @Resource
    private MallPrizeLotteryService mallPrizeLotteryService;
    @Resource
    private MallOrderWinnerService mallOrderWinnerService;
    @Resource
    private MallOrderProvider mallOrderProvider;

    /**
     * @description: 用户端购买成功
     * @author: Wade
     * @date: 2/21/23 4:31 PM
     * @param: [mallOrder]
     * @return: void
     **/
    @RabbitListener(queuesToDeclare = @Queue(value = MqConstants.MallOrder.Buy_Success, durable = "true"))
    public void buySuccess(String mallOrder) {
        try {
            log.info("商品订单消息跟踪buySuccess：" + mallOrder);
            MallPrizeOrder order = JSON.parseObject(mallOrder, MallPrizeOrder.class);
            MallPrizeLottery mallPrizeLottery = mallPrizeLotteryService.buySuccess(order);
            if(Objects.nonNull(mallPrizeLottery)) {
                mallOrderProvider.sendPrize(JSON.toJSONString(mallPrizeLottery));
            }
        } catch (Exception e) {
            log.error("[用户端购买成功消息消费异常] msg = {}", mallOrder, e);
        }
    }

    /**
     * @description: 抽奖完成消息
     * @author: Wade
     * @date: 2/21/23 4:31 PM
     * @param: [mallOrder]
     * @return: void
     **/
    @RabbitListener(queuesToDeclare = @Queue(value = MqConstants.MallOrder.Prize_Success, durable = "true"))
    public void prize(String mallOrder) {
        try {
            log.info("商品订单消息跟踪prize：" + mallOrder);
            MallPrizeLottery lottery = JSON.parseObject(mallOrder, MallPrizeLottery.class);
            mallOrderWinnerService.prize(lottery);
        } catch (Exception e) {
            log.error("[抽奖完成消息消费异常] msg = {}", mallOrder, e);
        }
    }

    /**
     * @description: 退款消息
     * @author: Wade
     * @date: 2/21/23 4:31 PM
     * @param: [mallOrder]
     * @return: void
     **/
    @RabbitListener(queuesToDeclare = @Queue(value = MqConstants.MallOrder.Prize_Refund, durable = "true"))
    public void refund(String mallOrder) {
        try {
            log.info("商品订单消息跟踪refund：" + mallOrder);
            MallPrizeOrder order = JSON.parseObject(mallOrder, MallPrizeOrder.class);
            mallPrizeOrderService.refund(order);
        } catch (Exception e) {
            log.error("[抽奖完成消息消费异常] msg = {}", mallOrder, e);
        }
    }
}
